package edu.ncst.baseweb.service.impl.resinfo;

import cn.wintersun.basecommon.common.Result;
import edu.ncst.baseweb.dao.res.ResInfo;
import edu.ncst.baseweb.dao.res.ResRecord;
import edu.ncst.baseweb.dto.JwtUser;
import edu.ncst.baseweb.mapper.ResRecordMapper;
import edu.ncst.baseweb.service.intrer.resinfo.ResRecordService;
import edu.ncst.baseweb.utils.GetUserinfoUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ResRecordImpl implements ResRecordService {
        @Autowired
        private ResRecordMapper resRecordMapper;
        @Autowired
        private GetUserinfoUtil getUserinfoUtil;

        public Result<IPage<ResRecord>> list(ResRecord resRecord){
            QueryWrapper<ResRecord> queryWrapper = Wrappers.query();
            JwtUser jwtUser = getUserinfoUtil.getUser();
            queryWrapper.eq(ResRecord.COL_USERID,jwtUser.getId());

            Page<ResRecord> resPage = new Page<>(resRecord.getCurrent(), resRecord.getPageSize());
            Page<ResRecord> resRecordPagePage = resRecordMapper.selectPage(resPage, queryWrapper);

            List<ResRecord> records = resRecordPagePage.getRecords();
            for (ResRecord record : records){
                if(record.getResId()!=null){
                    //添加resinfo表中的申请时间 状态 地址字段
                    ResInfo resInfo1=resRecordMapper.getResInfoByResId(record.getResId());
                    record.setLocation(resInfo1.getLocation());
                    record.setState(resInfo1.getState());
                    record.setReleaseTime(resInfo1.getReleaseTime());
                }
            }
            return Result.ok(resRecordPagePage);
        }
        public Result<Object> cancebook(ResRecord resRecord){
            //resRecordMapper.getResIdByResRecordId(resRecordId);
            ResInfo resInfo = resRecordMapper.getResInfoByResId(resRecord.getResId());
            //预约状态
            if (resInfo.getState()==0){
                resRecordMapper.deleteById(resRecord.getId());
                return Result.failure(200, "取消成功！");
            } else if (resInfo.getState()==1) {
                return Result.failure(500, "该课程已经确定");
            } else if (resInfo.getState()==2) {
                return Result.failure(200, "该课程已经取消");
            }
            return null;
        }
}
